package yl.hs.bmipfull.controllers.production;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.production.ProductionTaskEntity;
import yl.hs.bmipfull.service.production.IProductShipOrderService;
import yl.hs.bmipfull.service.production.IProductionTaskService;
import yl.hs.bmipfull.service.production.ITransTaskService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.*;

/**
 * 作用:  调度控制层<br/>
 * 创建时间: 2021年06月21日 10时08分52秒 <br/>
 *
 * @author 谢东
 */
@Api(value = "产品发货单Web接口", tags = "生产管理——产品发货单操作")
@RequestMapping(value = "/api/dispatch")
@RestController
public class DispatchController extends ControllerBase {

    @Autowired
    IProductShipOrderService shipOrderService;

    @Autowired
    IProductionTaskService taskService;

    @Autowired
    ITransTaskService transTaskService;

    @ApiOperation(value = "查询生产进度", notes = "查询生产进度")
    @RequestMapping(value = "/getOfProgress/{productCateSN}", method = RequestMethod.GET)
    public WebResult getOfProgress(@ApiParam(value = "产品种类编号") @PathVariable String productCateSN) {
        try {
            Map<String, Object> map = new HashMap<>();
            Date date = new Date();
            double todayVolume = shipOrderService.getDayVolume(date, productCateSN);
            double monthVolume = shipOrderService.getMonthVolume(date, productCateSN);
            //获取生产中的生产任务单
            List<ProductionTaskEntity> taskList = taskService.queryTaskSc(productCateSN);
            double planVolume = 0;
            List<String> snList = new ArrayList<>();
            if (taskList != null) {
                for (ProductionTaskEntity entity : taskList) {
                    planVolume = planVolume + Double.parseDouble(entity.getQuantity().toString());
                    snList.add(entity.getSn());
                }
                map.put("taskCount", taskList.size());
            } else {
                map.put("taskCount", 0);
            }
            //获取生产中的生产任务单的完成方量
            double completeVolume = shipOrderService.getVolumeByTaskSN(snList);
            double NotCompleteVolume = planVolume - completeVolume;
            //处理返回值
            map.put("todayVolume", todayVolume);
            map.put("monthVolume", monthVolume);
            map.put("planVolume", planVolume);
            map.put("completeVolume", completeVolume);
            map.put("NotCompleteVolume", NotCompleteVolume);
            return success("查询成功", map);
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询生产任务明细", notes = "根据生产任务单查询生产任务明细")
    @RequestMapping(value = "/GetTask/{taskSn}", method = RequestMethod.GET)
    public WebResult GetTask(@ApiParam(value = "编号") @PathVariable String taskSn) {
        try {
            return success("查询成功", taskService.getTaskDetailBySN(taskSn));
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "根据生产任务单查询发货信息", notes = "根据生产任务单查询发货信息")
    @RequestMapping(value = "/GetShipOrder", method = RequestMethod.POST)
    public WebResult GetShipOrderByTaskSN(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", shipOrderService.getListByProductionTaskSN(pageQuery));
        } catch (Exception e) {
            log.error("获取生产任务发货单时出错",e);
            return fail("数据未保存");
        }

    }

    @ApiOperation(value = "查询生产任务派车信息", notes = "根据生产任务单查询派车信息")
    @RequestMapping(value = "/GetTranTask", method = RequestMethod.POST)
    public WebResult GetTranTaskByTaskSN(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", transTaskService.getTranListByTaskSN(pageQuery));
        } catch (Exception e) {
           log.error("获取生产任务派车时出错",e);
            return fail("数据未保存");
        }

    }

   // public WebResult
}